package com.gk.carshopsys.service;

import com.gk.carshopsys.dao.CarDao;
import com.gk.carshopsys.dao.CarOrderDao;
import com.gk.carshopsys.dao.CustDao;
import com.gk.carshopsys.dao.EmpDao;
import com.gk.carshopsys.pojo.Car;
import com.gk.carshopsys.pojo.CarOrder;
import com.gk.carshopsys.pojo.CarOrderImport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class CarOrderServiceImpl implements CarOrderService{

    @Autowired
    CarOrderDao carOrderDao;
    @Autowired
    CarDao carDao;
    @Autowired
    CustDao custDao;
    @Autowired
    EmpDao empDao;

    @Override
    public int addCarByList(List<CarOrderImport> list) {
        return carOrderDao.addCarOrderByList(list);
    }

    @Override
    public List<CarOrder> findCarOrder(HashMap<String,String> params) {
        String car = params.get("car");
        String emp = params.get("emp");
        String ordernum = params.get("ordernum");
        String cust = params.get("cust");
        String state = params.get("state");
        String start_time = params.get("start_time");
        String end_time = params.get("end_time");
        return carOrderDao.findCarOrder(params);
    }


/**
 *  新增订单，库存处理
 * @return
 * -1：没有这个车型
 * -2：没有这个客户
 * -3：没有这个员工
 * -4：库存不足
 *
 */

    @Override
    public int addCarOrder(Long carid, Long custid, String empnum, Integer quantity, String state, String remarks) {
        CarOrder carorder=new CarOrder();

        if(carDao.findCarId(carid).isEmpty()){
            return -1;
        }
        if (custDao.findCustId(custid).isEmpty()){
            return -2;
        }
        if (empDao.findEmpByEmpno(empnum).isEmpty()){
            return -3;
        }else{
            Integer empid=empDao.findEmpByEmpno(empnum).get(0).getId();
            carorder.setEmpid(empid);
        }

        //获取库存
        Long stock = carDao.findQuantityById(carid);

        if(stock<quantity){
            return -4;
        }else{
            Car car = new Car();
            car.setId(carid);
            car.setQuantity(stock-quantity);
            carDao.updateQuantityById(car);
        }

        carorder.setCarid(carid);
        carorder.setCustid(custid);
        carorder.setQuantity(quantity);
        carorder.setState(state);
        carorder.setRemarks(remarks);

        String now = new Date(System.currentTimeMillis()).toString();

        carorder.setCreate_time(now);
        carorder.setUpdate_time(now);

        Date date = new Date(System.currentTimeMillis());

//获取年月日
        java.text.SimpleDateFormat sformat = new java.text.SimpleDateFormat();
        sformat.applyPattern("yyyy");
        String year = sformat.format(date);
        sformat.applyPattern("MM");
        String mouth = sformat.format(date);
        sformat.applyPattern("dd");
        String day = sformat.format(date);

        Integer empid=empDao.findEmpByEmpno(empnum).get(0).getId();
//        生成3位随机数
        int randow=(int)(Math.random()*(900+100));
        String ordernum =  year+mouth+day+carid+empid+custid+randow;

        carorder.setOrdernum(ordernum);


        int result = carOrderDao.addCarOrder(carorder);
        return result;
    }

    @Override
    public int updateCarOrderById(CarOrder carOrder,Long quantity) {
        if(carOrder.getState().equals("已取消")){
            //获取库存
            Long stock = carDao.findQuantityById(carOrder.getCarid());
            Car car = new Car();

            car.setId(carOrder.getCarid());
            car.setQuantity(stock+quantity);
            carDao.updateQuantityById(car);

        }
        String now = new Date(System.currentTimeMillis()).toString();
        carOrder.setUpdate_time(now);
        System.out.println(carOrder);
        return carOrderDao.updateCarOrderById(carOrder);
    }

    @Override
    public int delCarOrder(List<Integer> carorder) {
        return carOrderDao.delCarOrder(carorder);
    }


}
